<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.js"></script>
</head>
<body>
  <div class="container">
    <h2 class="page-header">基本使用</h2>
    <button class="btn btn-primary">发送GET请求</button>
    <button class="btn btn-warning">发送POST请求</button>
    <button class="btn btn-success">发送PUT请求</button>
    <button class="btn btn-danger">发送DELETE请求</button>
  </div>
  <script>
    //获取按钮
    const btns=document.querySelectorAll('button')
    //第一个
    btns[0].onclick=function(){
      //发送AJAX请求
      axios({
        //请求类型
        method:'GET',
        //URL
        url:'http://localhost:3000/posts/2'
      }).then(res=>{
        console.log(res);
      })
    },
    //添加一篇新的文章
    btns[1].onclick=function(){
      //发送AJAX请求
      axios({
        //请求类型
        method:'POST',
        //URL
        url:'http://localhost:3000/posts/',
        //设置请求体
        data: {
          title:"我今天不开心",
          author:"小橋"
        },
      }).then(res=>{
        console.log(res);
      })
    },
    //更新数据
    btns[2].onclick=function(){
      //发送AJAX请求
      axios({
        //请求类型
        method:'PUT',
        //URL
        url:'http://localhost:3000/posts/3',
        //设置请求体
        data: {
          title:"我今天不开心",
          author:"小明"
        },
      }).then(res=>{
        console.log(res);
      })
    } 
    //删除数据
    btns[3].onclick=function(){
      //发送AJAX请求
      axios({
        //请求类型
        method:'delete',
        //URL
        url:'http://localhost:3000/posts/3'
      }).then(res=>{
        console.log(res);
      })
    } 
  </script>
</body>
</html>